/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.controller.hvac;

import android.car.VehicleAreaSeat;
import android.car.VehicleAreaType;
import android.car.VehiclePropertyIds;
import android.car.hardware.property.CarPropertyManager;

import com.ecarx.systemui.plugin.controller.CarSignalController;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class HvacBaseController {

    private static final String TAG = "HvacBaseController";
    public static final int AREA_SEAT_GLOBAL = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT | VehicleAreaSeat.SEAT_ROW_2_LEFT | VehicleAreaSeat.SEAT_ROW_2_RIGHT | VehicleAreaSeat.SEAT_ROW_2_CENTER;

    public static final int AREA_SEAT_ROW_1 = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT;


    public boolean isPropertyAvailable(int propertyId, int area) {
        boolean isOk = CarSignalController.checkSelf();
        if (!isOk) {
            return false;
        }
        try {
            CarPropertyManager carPropertyManager = CarSignalController.getInstance().getCarPropertyManager();
            boolean isAvailable = carPropertyManager.isPropertyAvailable(propertyId, area);
            LogUtils.d(TAG, "propertyId:" + propertyId + "  area:" + area + "  isAvailable:" + isAvailable);
            return isAvailable;
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }

        return false;
    }

    public boolean isDriverHvacTemperatureSetAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_TEMPERATURE_SET, VehicleAreaSeat.SEAT_ROW_1_LEFT);
    }

    public boolean isPassengerHvacTemperatureSetAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_TEMPERATURE_SET, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
    }

    public boolean isHvacDualOnAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_DUAL_ON, AREA_SEAT_GLOBAL);
    }

    public boolean isHVACPowerOnAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_POWER_ON, AREA_SEAT_GLOBAL);
    }

    public boolean isHVACTemperatureDisplayUnitsAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_TEMPERATURE_DISPLAY_UNITS, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
    }


    public boolean isHvacAutoOnAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_AUTO_ON, AREA_SEAT_ROW_1);
    }


    public boolean isHvacFanSpeedAvailable() {
        return isPropertyAvailable(VehiclePropertyIds.HVAC_FAN_SPEED, AREA_SEAT_ROW_1);
    }

    public void setHVACPowerOn(boolean isOn) {
        boolean isOk = CarSignalController.checkSelf();
        if (!isOk) {
            return;
        }
        try {
            CarPropertyManager carPropertyManager = CarSignalController.getInstance().getCarPropertyManager();
            if (isHVACPowerOnAvailable()) {
                carPropertyManager.setBooleanProperty(VehiclePropertyIds.HVAC_POWER_ON, AREA_SEAT_GLOBAL, isOn);
            }
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
    }

    public boolean isHVACPowerOn() {
        boolean isOk = CarSignalController.checkSelf();
        if (!isOk) {
            return false;
        }
        try {
            CarPropertyManager carPropertyManager = CarSignalController.getInstance().getCarPropertyManager();
            if (isHVACPowerOnAvailable()) {
                return carPropertyManager.getBooleanProperty(VehiclePropertyIds.HVAC_POWER_ON, AREA_SEAT_GLOBAL);
            }
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
        return false;
    }
}
